package com.yu1996.gulimall.member.controller;

import com.yu1996.common.exception.BizCodeEnume;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.R;
import com.yu1996.gulimall.member.entity.MemberEntity;
import com.yu1996.gulimall.member.exception.PhoneExistException;
import com.yu1996.gulimall.member.exception.UsernameExistException;
import com.yu1996.gulimall.member.service.MemberService;
import com.yu1996.gulimall.member.vo.UserLoginVo;
import com.yu1996.gulimall.member.vo.UserRegisterVo;
import java.util.Arrays;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会员
 *
 * @author yu
 * @email 1798121567@qq.com
 * @date 2021-08-06 12:06:51
 */
@RestController
@RequestMapping("member/member")
public class MemberController {

  @Autowired private MemberService memberService;

  /** 登录 */
  @PostMapping("/login")
  public R login(@RequestBody UserLoginVo userLoginVo) {
    MemberEntity memberEntity = memberService.login(userLoginVo);
    if (memberEntity != null) {
      // 返回不为空  即为登录成功
      return R.ok().put("data", memberEntity);
    } else {
      // 返回登录失败  --用户名或密码错误   此处不多做处理，统一返回一个错误信息
      return R.error(
          BizCodeEnume.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(),
          BizCodeEnume.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
    }
  }
  /** 注册 */
  @PostMapping("/regist")
  public R regist(@RequestBody UserRegisterVo userRegisterVo) {
    try {
      memberService.regist(userRegisterVo);
    } catch (UsernameExistException e) { // 捕获异常  进行分类返回给调用者
      return R.error(
          BizCodeEnume.USERNAME_EXIST_EXCEPTION.getCode(),
          BizCodeEnume.USERNAME_EXIST_EXCEPTION.getMsg());
    } catch (PhoneExistException e) {
      return R.error(
          BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),
          BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
    }
    return R.ok();
  }

  /** 列表 */
  @RequestMapping("/list")
  // @RequiresPermissions("member:member:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = memberService.queryPage(params);

    return R.ok().put("page", page);
  }

  /** 信息 */
  @RequestMapping("/info/{id}")
  // @RequiresPermissions("member:member:info")
  public R info(@PathVariable("id") Long id) {
    MemberEntity member = memberService.getById(id);

    return R.ok().put("member", member);
  }

  /** 保存 */
  @RequestMapping("/save")
  // @RequiresPermissions("member:member:save")
  public R save(@RequestBody MemberEntity member) {
    memberService.save(member);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  // @RequiresPermissions("member:member:update")
  public R update(@RequestBody MemberEntity member) {
    memberService.updateById(member);

    return R.ok();
  }

  /** 删除 */
  @RequestMapping("/delete")
  // @RequiresPermissions("member:member:delete")
  public R delete(@RequestBody Long[] ids) {
    memberService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }
}
